package com.rtf.framework.web.mvc.config.secure.cookie;


import com.rtf.framework.web.http.AppIBaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 跨域cookie共享，将共享cookie信息
 *
 * @Author : liupeng
 * @Date : 2019-03-15
 * @Modified By
 */
@Slf4j
@RequestMapping("/api/global/cookie-user.do")
public class AppCookieUserCallback extends AppIBaseController {

    @RequestMapping(method= RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> userCallback(HttpServletRequest request) {

        String userId = "" ;
        try{
            userId = super.getLoginUserId();
        }catch( Exception e ){
            log.error( "/api/global/cookie-user.do获取登录用户信息失败" ) ;
        }

        StringBuffer appUser = new StringBuffer(
                "(function(obj){\n" +
                        "        obj.appUserId = {\n" +
                        "          userId : '" + userId + "'\n" +
                        "        }\n" +
                        "      })(window) ;"
        );
        return ResponseEntity.status(HttpStatus.OK)
                .contentType(MediaType.valueOf("application/javascript"))
                .body(appUser.toString());
    }
}
